Bmob.initialize("9fdb585b2da678c0284f514fc017723e", "095134f9e8d554812cbb4eece88435c0");
var my_results;
var now_id;

function add() {
	var title = $("#title").val();
	var desc = $("#desc").val();
	var content = $("#content").val();
	if (isempty(title) || isempty(content)) {
		return;
	}

	var A_ProGuard = Bmob.Object.extend("A_ProGuard");
	var proguard = new A_ProGuard();
	proguard.save({
		title: title,
		desc: desc,
		content: content
	}, {
		success: function(object) {
			addonetypes(object);
			alert("提交成功");
		},
		error: function(model, error) {
			alert("提交失败" + error.code + " " + error.message);
		}
	});
}

function modify() {
	var title = $("#title").val();
	var desc = $("#desc").val();
	var content = $("#content").val();
	if (isempty(title) || isempty(content)) {
		return;
	}
	var proguard = getResult();
	if (proguard == undefined) {
		add();
		return;
	}
	proguard.set("title", $("#title").val());
	proguard.set("desc", $("#desc").val());
	proguard.set("content", $("#content").val());

	proguard.save(null, {
		success: function(object) {
			alert("修改成功");
		},
		error: function(model, error) {
			alert("修改失败" + error.code + " " + error.message);
		}
	});
}

function load() {
	var A_ProGuard = Bmob.Object.extend("A_ProGuard");
	var query = new Bmob.Query(A_ProGuard);
	$("#ps").empty();
	// 查询所有数据
	query.find({
		success: function(results) {
			// 循环处理查询到的数据
			// addtops(results);
			addalltypes(results);
		},
		error: function(error) {
			alert("查询失败: " + error.code + " " + error.message);
		}
	});
}

function clearTxt() {
	now_id = "";
	$("#title").val("");
	$("#desc").val("");
	$("#content").val("");
}

$(document).ready(function() {
	$('.dropdown-toggle').dropdown();
	$("#defaulttype").on("click", aclick);
	load();
	listenps();
});


function isempty(obj) {
	if (obj == undefined || $.trim(obj).length == 0) {
		return true;
	}
	return false;
}

function listenps() {
	$("#ps").change(function() {
		if (my_results == undefined)
			return;
		var object = my_results[$("#ps").val()];
		$("#title").val(object.get("title"));
		$("#desc").val(object.get("desc"));
		$("#content").val(object.get("content"));
	});
}

function addtops(results) {
	for (var i = 0; i < results.length; i++) {
		var object = results[i];
		var option = $("<option>").val(i).text(object.get("title"));     
		$("#ps").append(option);
	}
}

/*添加多个元素*/
function addalltypes(results) {
	my_results = results;

	/*清理元素*/
	$("#alltypes").children().each(function(position, dom) {
		if (position != 0) {
			$(dom).remove();
		}
	});

	$.each(results, function(position, object) {
		addonetype(object);
	});
}

/*添加一个元素*/
function addonetypes(object) {
	my_results.push(object);
	addonetype(object);
}

/*添加一个界面元素*/
function addonetype(object) {
	/* <li><a onclick="aa(this)" href="#" item-data="1">请选择</a></li> */
	var li = $("<li>");
	var a = $("<a>");
	a.attr("href", "#");
	a.attr("item-data", object.id);
	a.text(object.get("title"));
	a.on("click", aclick);
	li.append(a);

	li.appendTo($("#alltypes"))
}

/*元素点击事件*/
function aclick(event) {
	var obj = event.target;
	var id = $(obj).attr("item-data");
	var text = $(obj).html();
	now_id = id;
	$("#alltype_title").text(text);

	var result = getResult();
	if (result) {
		$("#title").val(result.get("title"));
		$("#desc").val(result.get("desc"));
		$("#content").val(result.get("content"));
	}else{
		clearTxt();
	}
}

function getResult() {
	var proguard = undefined;
	$.each(my_results, function(position, object) {
		if (object.id === now_id) {
			proguard = object;
			return false;
		}
	});
	return proguard;
}